package com.zhdl.modules.common.production.web.controller;

import com.zhdl.common.util.WebResult;
import com.zhdl.modules.common.production.entity.ProductionRecord;
import com.zhdl.modules.common.production.service.ProductionRecordService;
import com.zhdl.modules.common.production.service.dto.ProductionReocrdQueryCriteria;
import com.zhdl.modules.common.production.web.request.ProductionRecordReq;
import com.zhdl.modules.common.production.web.request.ProductionStatisticsReq;
import com.zhdl.modules.common.production.web.response.ProductionStatisticsResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.zhdl.modules.common.production.service.impl.ProductionRecordServiceImpl.*;

@Slf4j
@RestController
@RequestMapping(path = "/records/production", produces = MediaType.APPLICATION_JSON_VALUE)
public class ProductionRecordController {

    @Resource
    private ProductionRecordService productionRecordService;

    /**
     * 测试接口
     *
     * @return 测试服务是否启动
     */
    @GetMapping("/demo")
    public WebResult<String> InterfaceDebugging() {
        return WebResult.okResult("当前接口通讯正常");
    }

    @GetMapping("/production/findAllList")
    public WebResult<List<ProductionRecord>> findAllProductionRecordsList() {
        List<ProductionRecord> productionRecords = productionRecordService.selectAll();
        return WebResult.okResult(productionRecords);
    }

    @PostMapping("/production/findAllPage")
    public WebResult<Page<ProductionRecord>> findAllProductionRecordsPage(@RequestBody ProductionRecordReq request) {
        ProductionReocrdQueryCriteria queryCriteria = request.getQueryCriteria();
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(request.getPageNumber(), request.getPageSize(), sort);
        Page<ProductionRecord> allPage = productionRecordService.findAllPage(queryCriteria, pageable);
        return WebResult.okResult(allPage);
    }

    /**
     * 查询项目总产量
     *
     * @param request 型号相关信息
     * @return /
     */
    @PostMapping("/selectProductionStatics")
    public WebResult<ProductionStatisticsResp> getProductionStatistics(@RequestBody ProductionStatisticsReq request) {
        LocalDate currentDate = LocalDate.now();    // 获取当前日期
        DateTimeFormatter formatterDay = DateTimeFormatter.ofPattern("yyyy-MM-dd");    // 定义日期格式
        DateTimeFormatter formatterMonth = DateTimeFormatter.ofPattern("yyyy-MM");    // 定义日期格式
        DateTimeFormatter formatterYear = DateTimeFormatter.ofPattern("yyyy");    // 定义日期格式
        String today = currentDate.format(formatterDay);   // 格式化当前日期为字符串
        String month = currentDate.format(formatterMonth);
        String year = currentDate.format(formatterYear);
        int quarter = (currentDate.getMonthValue() - 1) / 3 + 1;    //获取当前季度
        String[] fullQuarter = getMonthsInQuarter(year, quarter); //获取全部季度月份

        List<Integer> formulaIds = new ArrayList<>();//获取套餐的Id
        formulaIds.add(request.getFormulaId());
        ProductionReocrdQueryCriteria queryCriteria = new ProductionReocrdQueryCriteria();//查询条件
        queryCriteria.setFormulaId(formulaIds);
        ProductionStatisticsResp resp = new ProductionStatisticsResp();
        resp.setFormulaId(formulaIds.get(0));

        List<ProductionRecord> list = productionRecordService.findList(queryCriteria);
        Map<String, Integer> productionByTime = getProductionByTime(list);
        Integer nowMonth = productionByTime.get(month);
        Integer quarterList1 = productionByTime.get(fullQuarter[0]);
        Integer quarterList2 = productionByTime.get(fullQuarter[1]);
        Integer quarterList3 = productionByTime.get(fullQuarter[2]);
        Integer quarterList = quarterList1 + quarterList2 + quarterList3;
        int nowYear = 0;
        for (String s : productionByTime.keySet()) {
            Integer i = productionByTime.get(s);
            nowYear = nowYear + i;
        }
        queryCriteria.setCreateDate(today);
        List<ProductionRecord> todayList = productionRecordService.findList(queryCriteria);
        int nowToday = 0;
        for (ProductionRecord productionRecord : todayList) {
            nowToday = nowToday + productionRecord.getProductionOutput();
        }

        resp.setDailyProduction(nowToday);
        resp.setMonthlyProduction(nowMonth);
        resp.setQuarterlyProduction(quarterList);
        resp.setAnnualOutput(nowYear);
        resp.setTotalOutput(getProductionByAll(list));
        return WebResult.okResult(resp);
    }


}